package com.xuxu.blog.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;


/**
 * @className: EmailUtils
 * @author: Maxuxu
 * @date: 2022/8/22 14:33
 * @description: 邮件发送的工具类
 */

@Component
@Slf4j
public class EmailUtils {
    @Resource
    private JavaMailSender javaMailSender;
    @Autowired
    TemplateEngine templateEngine;

    final String emailFrom = "870839344@qq.com";

    /**
     * 发送给指定的用户邮件（包含附件 - 比如图片）
     * @param setTo
     * @param getSubject
     * @param content
     */
    @Async
    public void sendEmailWithFile(String setTo,String getSubject,String content,String filePath){
        MimeMessage mMessage = javaMailSender.createMimeMessage();//创建邮件对象
        MimeMessageHelper mMessageHelper;
        Properties prop = new Properties();
        String from;
        try {
            //从配置文件中拿到发件人邮箱地址
            prop.load(this.getClass().getResourceAsStream("/email.properties"));
            from = prop.get("mail.smtp.username") + "";
            mMessageHelper = new MimeMessageHelper(mMessage, true);
            mMessageHelper.setFrom(from);//发件人邮箱
            mMessageHelper.setTo(setTo);//收件人邮箱
            mMessageHelper.setSubject(getSubject);//邮件的主题
            mMessageHelper.setText(content, true);//邮件的文本内容，true表示文本以html格式打开
            File file = new File(filePath);//在邮件中添加一张图片
            FileSystemResource resource = new FileSystemResource(file);
            //mMessageHelper.addInline("fengye", resource);//这里指定一个id,在上面引用
            mMessageHelper.addAttachment("beauty.png", resource);//在邮件中添加一个附件
            javaMailSender.send(mMessage);//发送邮件
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送简单的邮件
     * @param target
     * @param subject
     * @param content
     */
    @Async
    public void sendSimpleEmail(String target,String subject,String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        //发件人的邮箱地址
        message.setFrom(emailFrom);
        message.setTo(target);
        //邮件主题
        message.setSubject(subject);
        if (content==null){
            content = "未知内容";
        }
        message.setText(content);
        log.debug("发送了一封邮件：{}",subject);
        //发送邮件
        javaMailSender.send(message);
    }
    @Async
    public void sendHtmlMail(String target,String subject,String content) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(emailFrom);
        helper.setTo(target);
        helper.setSubject(subject);
        helper.setText(content,true);
        javaMailSender.send(mimeMessage);
    }

    /**
     * 通过模板引擎发送错误通知
     * @param target
     * @param subject
     * @param content
     * @throws MessagingException
     */
    @Async
    public void sendHtmlMails(String target,String subject,String type,String content) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(emailFrom);
        helper.setTo(target);
        helper.setSubject(subject);
        Context context = new Context();
        context.setVariable("code",subject);
        context.setVariable("type",type);
        context.setVariable("info", content);
        context.setVariable("create",new Date());
        String process = templateEngine.process("html/error", context);
        helper.setText(process,true);
        javaMailSender.send(mimeMessage);
    }

    /**
     * 注册成功发送邮件
     * @param target
     * @throws MessagingException
     */
    @Async
    public void sendsignupMails(String target,String username,String role) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(emailFrom);
        helper.setTo(target);
        helper.setSubject("注册成功通知");
        Context context = new Context();
        context.setVariable("role",role);
        context.setVariable("username", username);
        context.setVariable("date",new Date());
        String process = templateEngine.process("html/signupSuccess", context);
        helper.setText(process,true);
        javaMailSender.send(mimeMessage);
    }
}

